package homework.Demo2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class CopyRunnable implements Runnable {
    private Punishment punishment;

    //获取线程名称
    public CopyRunnable(Punishment punishment) {
        this.punishment = punishment;
    }

    @Override
    public void run() {
        String threadName = Thread.currentThread().getName();

        //定义int变量,统计每个线程的书写次数
        int count = 0;
        while (true) {
            synchronized (this) {
                //没抄完
                if (punishment.getLeftCount() > 0) {
                    //获取剩余次数
                    int leftCount = punishment.getLeftCount();
                    System.out.println(threadName + "线程-抄写一遍" +
                            punishment.getCopyWords() + "。两人总共还要抄写" + (--leftCount) + "次");
                    punishment.setLeftCount(leftCount);
                    count++;
                } else {
                    break;
                }
            }
        }
        System.out.println(threadName+"线程-一共抄写了"+count+"次");
    }
}
